package servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 06-ServletContext练习
 */
public class Demo09Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断是否第一次请求
        Integer count = (Integer) getServletContext().getAttribute("count");
        if (null == count) {
            //是第一次请求
            count = 1;
        } else {
            //不是第一次请求
            count++;
        }
        getServletContext().setAttribute("count", count);

        System.out.println("访问次数为 : " + getServletContext().getAttribute("count"));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
